package com.zte.user.control;

import com.zte.user.model.Course;
import com.zte.user.model.Student;
import com.zte.user.model.Teacher;
import com.zte.user.model.Users;
import com.zte.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserAction {
    @Autowired
    private IUserService service;

    @RequestMapping("/login")
    @ResponseBody
    public List<Users> login(HttpSession session,Users users){
        System.out.println(users);
        System.out.println(1);
        List<Users> users1 = service.login(users);
        if(users1.size()>0){
            session.setAttribute("userSession",users1.get(0));
        }
        return users1;
    }

    @RequestMapping("/register")
    @ResponseBody
    public boolean register(String role,String username,String password,String gender,String clazz){
        boolean b = false;
        boolean b1 = false;
        boolean b2 = false;

        Users users = new Users();
        users.setUsername(username);
        if(role.equals("1")){
            Student student=new Student("3",username,gender,clazz,password);
            Users user=new Users(username,password,role);
            users.setRole(role);
            List<Users> all = service.findAll(users);
            if(all.size()>0){
                return b;
            }else {
                b1 = service.addStu(student);
                b2 = service.addUser(user);
                System.out.println(b1);
                System.out.println(b2);
                if (b1 == true && b2 == true) {
                    b = true;
                }
            }
        }if(role.equals("2")){
            Teacher teacher=new Teacher("1",username,gender,password);
            Users user=new Users(username,password,role);
            users.setRole(role);
            List<Users> all = service.findAll(users);
            if(all.size()>0){
                return b;
            }else {
                b1 = service.addTeacher(teacher);
                b2 = service.addUser(user);
                System.out.println(b1);
                System.out.println(b2);
                if (b1 == true && b2 == true) {
                    b = true;
                }
            }
        }
        return b;
    }

    @RequestMapping("/update")
    @ResponseBody
    public boolean register(HttpSession session,String username,String password){
        boolean b = false;
        boolean b1 = false;
        boolean b2 = false;

        Users userSession = (Users) session.getAttribute("userSession");
        String role=userSession.getRole();
        Users users = new Users();
        users.setUsername(userSession.getUsername());
        users.setPassword(password);
        users.setRole(role);
        if(role.equals("1")){
            Student student=new Student();
            student.setStuname(userSession.getUsername());
            student.setPassword(userSession.getPassword());
            Student allStudent = service.findAllStudent(student);
            allStudent.setPassword(password);
            b1 = service.updateStu(allStudent);
            b2 = service.updateUser(users);
            if(b1==true&&b2==true){
                b = true;
            }
        }else if(role.equals("2")){
            Teacher teacher=new Teacher();
            teacher.setTname(userSession.getUsername());
            teacher.setPassword(userSession.getPassword());
            Teacher allTeacher = service.findAllTeacher(teacher);
            allTeacher.setPassword(password);
            b1 = service.updateTeacher(allTeacher);
            b2 = service.updateUser(users);
            if(b1==true&&b2==true){
                b = true;
            }
        }else {
            users.setPassword(password);
            b = service.updateUser(users);
        }
        return b;
    }

    @RequestMapping("/lookstu")
    @ResponseBody
    public Student lookstu(HttpSession session){
        Users userSession = (Users) session.getAttribute("userSession");
        Student student=new Student();
        student.setStuname(userSession.getUsername());
        Student allStudent = service.findAllStudent(student);
        return allStudent;
    }
    @RequestMapping("/lookteapro")
    @ResponseBody
    public Teacher lookteapro(HttpSession session){
        Users userSession = (Users) session.getAttribute("userSession");
        Teacher teacher=new Teacher();
        teacher.setTname(userSession.getUsername());
        Teacher allTeacher = service.findAllTeacher(teacher);
        return allTeacher;
    }
    @RequestMapping("/lookteacher")
    @ResponseBody
    public Teacher lookteacher(HttpSession session,String tid){
        Teacher teacher=new Teacher();
        teacher.setTid(tid);
        Teacher allTeacher = service.findAllTeacher(teacher);
        return allTeacher;
    }
    @RequestMapping("/lookstudent")
    @ResponseBody
    public Student lookstudent(HttpSession session,String stuid){
        Student student=new Student();
        student.setStuid(stuid);
        Student allStudent = service.findAllStudent(student);
        return allStudent;
    }
    @RequestMapping("/lookstudent1")
    @ResponseBody
    public Student lookstudent1(HttpSession session,String stuname){
        Student student=new Student();
        student.setStuname(stuname);
        Student allStudent = service.findAllStudent(student);
        return allStudent;
    }
    @RequestMapping("/lookcourse")
    @ResponseBody
    public Course lookcourse(HttpSession session,String courseid){
        Course course=new Course();
        course.setCourseid(courseid);
        Course allCourse = service.findAllCourse(course);
        return allCourse;
    }
    @RequestMapping("/lookclazz")
    @ResponseBody
    public Course lookclazz(HttpSession session,String tname){
        Course course=new Course();
        course.setTname(tname);
        Course allCourse = service.findAllCourse(course);
        return allCourse;
    }


    @RequestMapping("/updateteacher")
    @ResponseBody
    public boolean updateteacher(HttpSession session,Teacher teacher){
        boolean b1 = false;
        boolean b2 = false;
        boolean b = false;
        Teacher teacher1= new Teacher();
        teacher1.setTid(teacher.getTid());
        Teacher allTeacher = service.findAllTeacher(teacher1);

            Users users = new Users();
            users.setUsername(allTeacher.getTname());
            users.setPassword(teacher.getPassword());
            users.setRole("2");
            b1=service.updateTeacherper(teacher);
            b2=service.updateUser(users);
            if(b1==true&&b2==true){
                b=true;
            }
            return b;
    }
    @RequestMapping("/updatestudent")
    @ResponseBody
    public boolean updatestudent(HttpSession session,Student student){
        boolean b1 = false;
        boolean b2 = false;
        boolean b = false;
        Student student1= new Student();
        student1.setStuid(student.getStuid());
        Student allStudent = service.findAllStudent(student1);
            Users users = new Users();
            users.setUsername(allStudent.getStuname());
            users.setPassword(student.getPassword());
            users.setRole("1");
            b1=service.updateStu1(student);
            b2=service.updateUser(users);
            if(b1==true&&b2==true){
                b=true;
            }
            return b;
    }
    @RequestMapping("/findallteacher")
    @ResponseBody
    public List<Teacher> findallteacher(HttpSession session){
        List<Teacher> teachers = service.findAllTeachers();
        return teachers;
    }
    @RequestMapping("/findallstu")
    @ResponseBody
    public List<Student> findallstu(HttpSession session){
        List<Student> students = service.findAllStudents();
        return students;
    }
@RequestMapping("/deletestu")
    @ResponseBody
    public boolean deletestu(HttpSession session,String stuid){
//        Users userSession = (Users) session.getAttribute("userSession");
    boolean b1 = false;
    boolean b2 = false;
    boolean b = false;

        Student student=new Student();
        student.setStuid(stuid);
        Student allStudent = service.findAllStudent(student);
    if(allStudent!=null){
        Users users=new Users();
        users.setUsername(allStudent.getStuname());
        b1=service.deleteUser(users);
        b2=service.deleteStudent(allStudent);
        if(b1==true&&b2==true){
            System.out.println(allStudent.getStuname()+"删除成功");
            b=true;
        }
    }
        return b;
    }
    @RequestMapping("/deleteteacherper")
    @ResponseBody
    public boolean deleteteacherper(HttpSession session,String tid){
//        Users userSession = (Users) session.getAttribute("userSession");
        boolean b1 = false;
        boolean b2 = false;
        boolean b = false;
        Teacher teacher=new Teacher();
        teacher.setTid(tid);
        Teacher teacher1 = service.findAllTeacher(teacher);
        if(teacher1!=null){
            Users users=new Users();
            users.setUsername(teacher1.getTname());
            b1=service.deleteUser(users);
            b2=service.deleteTeacher(teacher1);
            if(b1==true&&b2==true){
                System.out.println(teacher1.getTname()+"删除成功");
                b=true;
            }
        }

//        student.setStuname(userSession.getUsername());
//        Student allStudent = service.findAllStudent(student);
        return b;
    }
    @RequestMapping("/deletecourse")
    @ResponseBody
    public boolean deletecourse(HttpSession session,String courseid){
        boolean b = false;
        Course course=new Course();
        course.setCourseid(courseid);
        b = service.deleteCourse(course);
        return b;
    }

    @RequestMapping("/lookcourses")
    @ResponseBody
    public List<Course> lookcourses(HttpSession session){
        List<Course> allCourses = service.findAllCourses();
        return allCourses;
    }

    @RequestMapping("/addcourse")
    @ResponseBody
    public boolean addcourse(HttpSession session,Course course){
        boolean b = false;
        b = service.addCourse(course);
        return b;
    }
    @RequestMapping("/updatecourse")
    @ResponseBody
    public boolean updatecourse(HttpSession session,Course course){
        boolean b = false;
        b = service.updateCourse(course);
        return b;
    }

}
